package com.maxsalad.uc.dao;

import com.maxsalad.common.dao.BaseDao;
import com.maxsalad.uc.dto.DPlatformShopDto;
import com.maxsalad.uc.em.Platform;
import com.maxsalad.uc.entity.ApiParameterEntity;
import com.maxsalad.uc.entity.ApiParameterUtil;
import com.maxsalad.uc.pojo.TPlatformShopEntity;
import org.apache.commons.collections.MapUtils;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by 2014-400 on 2016-03-16.
 */
@Repository
public class ApiParameterDao extends BaseDao<TPlatformShopEntity> {
    public void refresh() throws Exception {
        List<ApiParameterEntity> list = queryAllApiParameter();
        ApiParameterUtil.setList(list);
    }

    public List<ApiParameterEntity> getParameterAll() throws Exception {
        List<ApiParameterEntity> list = ApiParameterUtil.getList();
        if (list == null || list.size() == 0) {
            list = queryAllApiParameter();
            ApiParameterUtil.setList(list);
            return list;
        }
        return list;
    }

    public List<ApiParameterEntity> getParameter(Platform platform) throws Exception {
        List<ApiParameterEntity> list = ApiParameterUtil.getList();
        if (list == null || list.size() == 0) {
            list = getParameterAll();
        }

        List<ApiParameterEntity> temp = new ArrayList<ApiParameterEntity>();
        for (ApiParameterEntity entity : list) {
            if (entity.getPlatform().toString().equals(platform.toString())) {
                temp.add(entity);
            }
        }

        return temp;
    }

    protected List<ApiParameterEntity> queryAllApiParameter() throws Exception {
        StringBuffer hql = new StringBuffer("select t.shopId,t.shopPid,t.baseUrl,t.appKey,t.appSecret,");
        hql.append("tPlatform.platformCode,tShop.shopName ");
        hql.append(" from TPlatformShopEntity t,TPlatformEntity tPlatform,TShopEntity tShop ");
        hql.append(" where 1=1 and t.platformId=tPlatform.id and t.shopId=tShop.id ");
        hql.append(" order by t.createTime desc ");
        Map resultMap = this.pageQueryByHql(hql.toString(), null);
        if (MapUtils.getLong(resultMap, "total", 0L) > 0) {
            List<Object[]> resultList = (List<Object[]>) resultMap.get("rows");
            if (resultList != null && resultList.size() > 0) {
                List<ApiParameterEntity> list = new ArrayList<ApiParameterEntity>();
                ApiParameterEntity entity = null;
                for (Object[] item : resultList) {
                    entity = new ApiParameterEntity();
                    entity.setShopId(Long.valueOf(item[0].toString()));
                    entity.setShopPid(item[1].toString());
                    entity.setBaseUrl(item[2].toString());
                    entity.setAppKey(item[3].toString());
                    entity.setAppSecret(item[4].toString());
                    for (Platform platform : Platform.values()) {
                        if (platform.toString().toLowerCase().equals(item[5].toString().toLowerCase())) {
                            entity.setPlatform(platform);
                            break;
                        }
                    }
                    entity.setShopName(item[6].toString());

                    list.add(entity);
                }

                return list;
            }
        }

        return null;
    }
}
